﻿using System;
using System.Linq;

using IdeaNMR.Web.Core;
using IdeaNMR.Shared;
using System.Web.UI.WebControls;

namespace IdeaNMR.Web
{
    public abstract class DesignNMRObject_BasePage<T> : DesignBaseObject_BasePage<T>
        where T : NMRObject, new()
    {
        protected NMRObjectFassade<T> NMRObjectInfo
        {
            get
            {
                return this.EntityFacade as NMRObjectFassade<T>;
            }
            set
            {
                this.EntityFacade = value;
            }
        }

        #region Page Life Cycle
        protected override void butOk_Click(object sender, EventArgs e)
        {
            base.butOk_Click(sender, e);

            this.AuthorsList.Authors = this.NMRObjectInfo.EntityObject.Authors.Where(aa => aa.Index >= 0).ToList();
        }

        #region Control Events
        // At least on author must be selected
        protected void ValidateAuthorsNumber(object source, ServerValidateEventArgs args)
        {
            args.IsValid = (int.Parse(args.Value) < 1) ? false : true;
        }
        #endregion
        #endregion

        protected override void InitiateObject()
        {
            this.NMRObjectInfo = (this.TargetID.HasValue)
                            ? new NMRObjectFassade<T>(this.TargetID.Value, ScrewTurnWikiFacade.Username)
                            : new NMRObjectFassade<T>(ScrewTurnWikiFacade.Username);
        }

        protected override void SetValidationParameters()
        {
            base.SetValidationParameters();

            this.tbContent.MaxLength = Sanitar.Content.MaxLength;
            this.revContent.Text = Sanitar.Content.ErrorMsg;
            this.revContent.ValidationExpression = Sanitar.Content.ValidationRegex;
        }
        protected override void UpdateFormData()
        {
            base.UpdateFormData();

            this.tbContent.Text = this.NMRObjectInfo.EntityObject.Content;

            this.AuthorsList.Authors = this.NMRObjectInfo.EntityObject.Authors.Where(aa => aa.Index>=0).ToList();
        }

        // ALISTAIR: Comments added 09/07/2011
        // Retrieve info from the web form
        protected override void CollectFormData()
        {
            // inherit form data info from IdeaNMR.Web.DesignBaseObject_BasePage class:
            // experiment name given by user
            // "remarks" text given by user
            // contents of the "scipt" tab entered by the user
            // version info specified by the user
            base.CollectFormData();

            // get the contents of the "pulse program" tab entered by the user
            this.NMRObjectInfo.EntityObject.Content = this.tbContent.Text;

            // get the list of authors associated with the pulse program
            this.NMRObjectInfo.EntityObject.Authors.Clear();
            foreach (var nA in this.AuthorsList.Authors)
            {
                NumberedAuthor a = new NumberedAuthor();
                a.User = nA.User;
                a.Index = nA.Index;
                a.Id = nA.Id;
                this.NMRObjectInfo.EntityObject.Authors.Add(a);
            }
        }

        #region Page Controls Declaration
        protected global::System.Web.UI.WebControls.TextBox tbContent;
        protected global::System.Web.UI.WebControls.RegularExpressionValidator revContent;

        protected global::IdeaNMR.Web.AuthorList AuthorsList;
        #endregion
    }
}